package com.wangsh.splider.web.back.controller;

import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangsh.splider.common.pojo.ApiResponse;
import com.wangsh.splider.common.pojo.ApiResponseEnum;
import com.wangsh.splider.common.util.ConstatFinalUtil;
import com.wangsh.splider.common.util.PageInfoUtil;
import com.wangsh.splider.common.util.QRcodeUtil;
import com.wangsh.splider.controller.BaseController;
import com.wangsh.splider.orders.pojo.AOrdersUsers;
import com.wangsh.splider.orders.service.IOrdersDbService;
import com.wangsh.splider.system.pojo.ASysIp;
import com.wangsh.splider.system.pojo.ASysPro;
import com.wangsh.splider.system.service.ISystemDbService;
import com.wangsh.splider.system.service.ISystemOperService;
import com.wangsh.splider.task.pojo.ATaskDesc;
import com.wangsh.splider.task.pojo.ATaskHistory;
import com.wangsh.splider.task.pojo.ATaskUsers;
import com.wangsh.splider.task.pojo.ATaskUsersEnum;
import com.wangsh.splider.task.service.ITaskDbService;
import com.wangsh.splider.users.pojo.AAdmins;
import com.wangsh.splider.users.service.IUsersDbService;

/**
 * 模板示例代码操作
 *
 * @author wangsh
 */
@Controller
@RequestMapping("/back/task/")
public class TaskBackController extends BaseController {
    @Resource
    private ITaskDbService taskDbService;
    @Resource
	private IUsersDbService usersDbService;

    /**
     * 用户概要列表页面
     *
     * @return
     */
    @RequestMapping("/usersList")
    public String usersList(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--usersList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        String ordersUsersId = request.getParameter("ordersUsersId");
        request.setAttribute("ordersUsersId", ordersUsersId);
        condMap.put("ordersUsersId", ordersUsersId);
        String taskId = request.getParameter("taskId");
        request.setAttribute("taskId", taskId);
        condMap.put("taskId", taskId);
		String id = request.getParameter("id");
        request.setAttribute("id", id);
        condMap.put("id", id);
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<ATaskUsers> response = this.taskDbService.findCondListUsersService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        return "/back/task/usersList";
    }

    /**
     * 打开添加用户概要页面
     *
     * @return
     */
    @RequestMapping("/usersInsert")
    public String usersInsert() {
        ConstatFinalUtil.SYS_LOGGER.info("--usersInsert--");
        return "/back/task/usersInsert";
    }

    /**
     * 添加用户概要提交操作
     *
     * @return
     */
    @RequestMapping(value = "/usersInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String usersInsertSubmit(String pubTimeStr, ATaskUsers users) {
        ConstatFinalUtil.SYS_LOGGER.info("--usersInsertSubmit--");
        /* 为对象赋值 */
        users.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        users.setCreateTime(new Date());
        users.setUpdateTime(new Date());
        /* 保存结果 */
        ApiResponse response = this.taskDbService.saveOneUsersService(users);
        ConstatFinalUtil.SYS_LOGGER.info("--用户概要添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 打开更新用户概要页面
     *
     * @return
     */
    @RequestMapping("/usersUpdate")
    public String usersUpdate(String id, String operType, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--usersUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskUsers> response = this.taskDbService.findOneUsersService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
        	/* 查询所有启用的朝代 */
    		condMap.clear();
    		ApiResponse<ATaskDesc> descResponse = this.taskDbService.findCondListDescService(null, condMap);
    		model.addAttribute("descResponse", descResponse.toJSON());
    		condMap.clear();
    		ApiResponse<AOrdersUsers> usersResponse = this.ordersDbService.findCondListUsersService(null, condMap);
    		model.addAttribute("usersResponse", usersResponse.toJSON());
            return "/back/task/usersUpdate";
        }
        return "/back/task/usersInfo";
    }

    /**
     * 添加用户概要提交操作
     *
     * @return
     */
    @RequestMapping(value = "/usersUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String usersUpdateSubmit(String id,
                                    HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--usersUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskUsers> response = this.taskDbService.findOneUsersService(condMap);
        /* 获取java对象 */
        ATaskUsers users = response.getDataOneJava();

        /* 接收参数 */
        String taskId = request.getParameter("taskId");
        String ordersUsersId = request.getParameter("ordersUsersId");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String status = request.getParameter("status");
        String pubTimeStr = request.getParameter("pubTimeStr");
        /* 设置属性 */
        users.setTaskId(Integer.valueOf(taskId));
        users.setOrdersUsersId(Integer.valueOf(ordersUsersId));
        users.setEmail(email);
        users.setPhone(phone);
        users.setStatus(Byte.valueOf(status));
        users.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        users.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = taskDbService.updateOneUsersService(users);
        ConstatFinalUtil.SYS_LOGGER.info("--用户概要添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }

    /**
     * 用户概要批量操作
     *
     * @return
     */
    @RequestMapping(value = "/usersBatch", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String usersBatch(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--usersBatch--");
        Map<String, Object> condMap = new HashMap<String, Object>();
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 从session中获取信息 */
        String[] ids = request.getParameterValues("ids");

        /* 操作类型 */
        String operType = request.getParameter("operType");
        if (ids != null) {
            int totalNum = ids.length;
            int succedNum = 0;
            int failedNum = 0;
            for (int i = 0; i < ids.length; i++) {
                String id = ids[i];
                if ("status".equalsIgnoreCase(operType)) {
                    String status = request.getParameter("status");
                    condMap.clear();
                    condMap.put("id", id);
                    ApiResponse<ATaskUsers> apiResponseDb = this.taskDbService.findOneUsersService(condMap);
                    ATaskUsers users = apiResponseDb.getDataOneJava();
                    /* 设置状态 */
                    users.setStatus(Byte.valueOf(status));
                    users.setPubTime(new Date());

                    ApiResponse<Object> usersClassResponse = this.taskDbService.updateOneUsersService(users);
                    if (usersClassResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus()) {
                        succedNum++;
                    } else {
                        failedNum++;
                    }
                }
            }

            StringBuffer sb = new StringBuffer();
            sb.append("总条数:" + totalNum + ";");
            sb.append("成功条数:" + succedNum + ";");
            sb.append("失败条数:" + failedNum + ";");
            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
            apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
        }
        return apiResponse.toJSON().toJSONString();
    }

    /**
     * 历史列表页面
     *
     * @return
     */
    @Resource
    private ISystemDbService sysService;

    @RequestMapping("/historyList")
    public String historyList(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /*
         * 查询数据库 所有的数据以JSON的形式返回
         */
        ApiResponse<ATaskHistory> response = this.taskDbService.findCondListHistoryService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        return "/back/task/historyList";
    }

    /**
     * 打开添加历史页面
     *
     * @return
     */
    @RequestMapping("/historyInsert")
    public String historyInsert(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyInsert--");
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /* 查询所有完成的用户 */
        condMap.clear();
        condMap.put("status", ATaskUsersEnum.STATUS_COMPLETEDORDER.getStatus());
        ApiResponse<ATaskUsers> taskUsersResponse = this.taskDbService.findCondListUsersService(null, condMap);
        model.addAttribute("taskUsersResponse", taskUsersResponse.toJSON());
        return "/back/task/historyInsert";
    }

    /**
     * 添加历史提交操作
     *
     * @return
     */
    @RequestMapping(value = "/historyInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String historyInsertSubmit(String pubTimeStr, ATaskHistory history) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyInsertSubmit--");
        /* 为对象赋值 */
        history.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        history.setCreateTime(new Date());
        history.setUpdateTime(new Date());
        /* 保存结果 */
        ApiResponse response = this.taskDbService.saveOneHistoryService(history);
        ConstatFinalUtil.SYS_LOGGER.info("--历史添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 打开更新历史页面
     *
     * @return
     */
    @RequestMapping("/historyUpdate")
    public String historyUpdate(String id, String operType, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskHistory> response = this.taskDbService.findOneHistoryService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            /* 查询所有完成的用户 */
            condMap.clear();
            ApiResponse<ATaskUsers> taskUsersResponse = this.taskDbService.findCondListUsersService(null, condMap);
            model.addAttribute("taskUsersResponse", taskUsersResponse.toJSON());
            condMap.clear();
            ApiResponse<ASysIp> sysIpResponse = this.sysService.findCondListSysIpService(null, condMap);
            model.addAttribute("sysIpResponse", sysIpResponse.toJSON());
            condMap.clear();
            ApiResponse<ASysPro> sysProResponse = this.sysService.findCondListSysProService(null, condMap);
            model.addAttribute("sysProResponse", sysProResponse.toJSON());
            
            return "/back/task/historyUpdate";
        }
        return "/back/task/historyInfo";
    }

    /**
     * 添加历史提交操作
     *
     * @return
     */
    @RequestMapping(value = "/historyUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String historyUpdateSubmit(String id,
                                      HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskHistory> response = this.taskDbService.findOneHistoryService(condMap);
        /* 获取java对象 */
        ATaskHistory history = response.getDataOneJava();

        /* 接收参数 */
        String taskUsersId = request.getParameter("taskUsersId");
        String ipId = request.getParameter("ipId");
        String sysProId = request.getParameter("sysProId");
        String os = request.getParameter("os");
        String broswer = request.getParameter("broswer");
        String status = request.getParameter("status");
        String pubTimeStr = request.getParameter("pubTimeStr");
        /* 设置属性 */
        history.setTaskUsersId(Integer.valueOf(taskUsersId));
        history.setIpId(Integer.valueOf(ipId));
        history.setSysProId(Integer.valueOf(sysProId));
        history.setOs(os);
        history.setBroswer(broswer);
        history.setStatus(Byte.valueOf(status));
        history.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        history.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = taskDbService.updateOneHistoryService(history);
        ConstatFinalUtil.SYS_LOGGER.info("--历史添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }

    /**
     * 历史批量操作
     *
     * @return
     */
    @RequestMapping(value = "/historyBatch", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String historyBatch(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--historyBatch--");
        Map<String, Object> condMap = new HashMap<String, Object>();
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 从session中获取信息 */
        String[] ids = request.getParameterValues("ids");

        /* 操作类型 */
        String operType = request.getParameter("operType");
        if (ids != null) {
            int totalNum = ids.length;
            int succedNum = 0;
            int failedNum = 0;
            for (int i = 0; i < ids.length; i++) {
                String id = ids[i];
                if ("status".equalsIgnoreCase(operType)) {
                    String status = request.getParameter("status");

                    condMap.clear();
                    condMap.put("id", id);
                    ApiResponse<ATaskHistory> apiResponseDb = this.taskDbService.findOneHistoryService(condMap);
                    ATaskHistory history = apiResponseDb.getDataOneJava();
                    /* 设置状态 */
                    history.setStatus(Byte.valueOf(status));
                    history.setPubTime(new Date());

                    ApiResponse<Object> historyClassResponse = this.taskDbService.updateOneHistoryService(history);
                    if (historyClassResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus()) {
                        succedNum++;
                    } else {
                        failedNum++;
                    }
                }
            }

            StringBuffer sb = new StringBuffer();
            sb.append("总条数:" + totalNum + ";");
            sb.append("成功条数:" + succedNum + ";");
            sb.append("失败条数:" + failedNum + ";");
            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
            apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
        }
        return apiResponse.toJSON().toJSONString();
    }

    /**
     * 任务描述列表页面
     *
     * @return
     */
    @RequestMapping("/taskDescList")
    public String taskDescList(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--taskDescList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);

        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);

        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<ATaskDesc> response = this.taskDbService.findCondListDescService(pageInfoUtil, condMap);

        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
       
        return "/back/task/descList";
    }

    /**
     * 打开添加任务描述页面
     *
     * @return
     */
    @RequestMapping("/taskDescInsert")
    public String taskDescInsert(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--descInsert--");
        return "/back/task/descInsert";
    }

    /**
     * 添加任务描述提交操作
     *
     * @return
     */
    @RequestMapping(value = "/taskDescInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String taskDescInsertSubmit(String pubTimeStr, ATaskDesc taskDesc,HttpServletRequest request) {
        ConstatFinalUtil.SYS_LOGGER.info("--taskDescInsertSubmit--");
        /* 为对象赋值 */
        taskDesc.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        taskDesc.setCreateTime(new Date());
        taskDesc.setUpdateTime(new Date());
        AAdmins aAdmins = (AAdmins) request.getSession().getAttribute("admins");
        taskDesc.setAdminsId(aAdmins.getId());

        /* 保存结果 */
        ApiResponse response = this.taskDbService.saveOneDescService(taskDesc);
        ConstatFinalUtil.SYS_LOGGER.info("--任务描述添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 打开更新任务描述页面
     *
     * @return
     */
    @RequestMapping("/taskDescUpdate")
    public String taskDescUpdate(String id, String operType, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--taskDescUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskDesc> response = this.taskDbService.findOneDescService(condMap);
//        ATaskDesc taskDesc = response.getDataOneJava();
//        if(taskDesc.getWebsite()!= null) {
//        	try {
//        		String path ="E:\\Hadoop17\\splider\\splider-web\\splider-web-back\\src\\main\\webapp\\QRcode.png";
//        	生成二维码并且生成一个png文件	
//    	    ImageIO.write(qrCodeUtil.operGetEncode(taskDesc.getWebsite()), "png",new File(path)) ;
//        	} catch (Exception e) {
//        		// TODO Auto-generated catch block
//        		e.printStackTrace();
//        	}
//        }
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
        	condMap.clear();
    		ApiResponse<AAdmins> adminsResponse = this.usersDbService.findCondListAdminsService(null, condMap);
    		model.addAttribute("adminsResponse", adminsResponse.toJSON());
            return "/back/task/descUpdate";
        }
        return "/back/task/descInfo";
    }
                  
    /**
     * 添加任务描述提交操作
     *
     * @return
     */
    @RequestMapping(value = "/taskDescUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String taskDescUpdateSubmit(String id,
                                       HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--taskDescUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<ATaskDesc> response = this.taskDbService.findOneDescService(condMap);
        /* 获取java对象 */
        ATaskDesc taskDesc = response.getDataOneJava();

        /* 接收参数 */
        String name = request.getParameter("name");
        String adminsId = request.getParameter("adminsId");
        String website = request.getParameter("website");
        String content = request.getParameter("content");
        String contentParam = request.getParameter("contentParam");
        String status = request.getParameter("status");
        String pubTimeStr = request.getParameter("pubTimeStr");

        /* 设置属性 */
        taskDesc.setName(name);
        taskDesc.setAdminsId(Integer.valueOf(adminsId));
        taskDesc.setWebsite(website);
        taskDesc.setContent(content);
        taskDesc.setContentParam(contentParam);
        taskDesc.setStatus(Byte.valueOf(status));
        taskDesc.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        taskDesc.setUpdateTime(new Date());


        /* 数据库操作 */
        ApiResponse dbRes = taskDbService.updateOneDescService(taskDesc);
        ConstatFinalUtil.SYS_LOGGER.info("--任务描述添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }
        
    /**
     * 任务描述批量操作
     *
     * @return
     */
    @RequestMapping(value = "/taskDescBatch", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String taskDescBatch(HttpServletRequest request, Model model) {
        ConstatFinalUtil.SYS_LOGGER.info("--taskDescBatch--");
        Map<String, Object> condMap = new HashMap<String, Object>();
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 从session中获取信息 */
        String[] ids = request.getParameterValues("ids");

        /* 操作类型 */
        String operType = request.getParameter("operType");
        if (ids != null) {
            int totalNum = ids.length;
            int succedNum = 0;
            int failedNum = 0;
            for (int i = 0; i < ids.length; i++) {
                String id = ids[i];
                if ("status".equalsIgnoreCase(operType)) {
                    String status = request.getParameter("status");

                    condMap.clear();
                    condMap.put("id", id);
                    ApiResponse<ATaskDesc> apiResponseDb = this.taskDbService.findOneDescService(condMap);
                    ATaskDesc taskDesc = apiResponseDb.getDataOneJava();
                    /* 设置状态 */
                    taskDesc.setStatus(Byte.valueOf(status));
                    taskDesc.setPubTime(new Date());

                    ApiResponse<Object> taskDescClassResponse = this.taskDbService.updateOneDescService(taskDesc);
                    if (taskDescClassResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus()) {
                        succedNum++;
                    } else {
                        failedNum++;
                    }
                }
            }

            StringBuffer sb = new StringBuffer();
            sb.append("总条数:" + totalNum + ";");
            sb.append("成功条数:" + succedNum + ";");
            sb.append("失败条数:" + failedNum + ";");
            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
            apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
        }
        return apiResponse.toJSON().toJSONString();
    }
    @RequestMapping("getQRCodeUrl")
	public void getQRCodeUrl(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
		ConstatFinalUtil.SYS_LOGGER.info("=====getQRCodeUrl=====");
		String content = request.getParameter("webUrl");
		response.setContentType("image/png");
		response.setHeader("Pragma", "No-cache");  
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expires", 0);  
		//存入会话session  
        HttpSession session = request.getSession(true);  
        session.setAttribute("randSess", content); 
        BufferedImage image =null;
        if(content.startsWith("https://")) {
        	image = QRcodeUtil.operGetEncode(content);
        }else {
        	image = QRcodeUtil.operGetEncode("https://"+content);
        }
		
		ImageIO.write(image, "png", response.getOutputStream());
	}
    @Resource
    private IOrdersDbService ordersDbService;
   
    
    /**
	 * task导入用户操作
	 * @return
	 */
	@RequestMapping(value = "/taskDescAdd" , produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String taskDescAdd(String id, HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--taskDescAdd--");
		
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "");
		ApiResponse<AOrdersUsers> apiResponse = this.ordersDbService.findCondListUsersService(null, map);
		List<AOrdersUsers> dataListJava = apiResponse.getDataListJava();
		int totalNum = dataListJava.size() ; 
		int succedNum = 0 ; 
		int failedNum = 0 ; 
		for (AOrdersUsers aOrdersUsers : dataListJava) {
			int id1=aOrdersUsers.getId();
			String email=aOrdersUsers.getEmail();
			String phone=aOrdersUsers.getPhone();
		    ATaskUsers taskUsers=new ATaskUsers();
			Map<String, Object> condMap =new HashMap<String, Object>();
			condMap.put("email", email);
			ApiResponse<ATaskUsers> apiResponse2 = this.taskDbService.findOneUsersService(condMap );
			ATaskUsers dataOneJava = apiResponse2.getDataOneJava();
			if(dataOneJava==null)
			{
				//查询不到就添加
				taskUsers.setTaskId(Integer.valueOf(id));
				taskUsers.setOrdersUsersId(id1);
				taskUsers.setEmail(email);
				taskUsers.setPhone(phone);
				taskUsers.setStatus(ATaskUsersEnum.STATUS_NOTSTARTED.getStatus());
				taskUsers.setCreateTime(new Date());
				taskUsers.setUpdateTime(new Date());
				taskUsers.setPubTime(new Date());
				this.taskDbService.saveOneUsersService(taskUsers);
				succedNum ++ ; 
			}else
			{
				//查询到有数据判断任务id是否一致
				int taskId = dataOneJava.getTaskId();
				if(id.equals(String.valueOf(taskId)))
				{
					taskUsers.setTaskId(Integer.valueOf(id));
					taskUsers.setOrdersUsersId(id1);
					taskUsers.setEmail(email);
					taskUsers.setPhone(phone);
					taskUsers.setUpdateTime(new Date());
					this.taskDbService.updateOneUsersService(taskUsers);
					failedNum ++ ;
				}else
				{
					//查询不到就添加
					taskUsers.setTaskId(Integer.valueOf(id));
					taskUsers.setOrdersUsersId(id1);
					taskUsers.setEmail(email);
					taskUsers.setPhone(phone);
					taskUsers.setStatus(ATaskUsersEnum.STATUS_NOTSTARTED.getStatus());
					taskUsers.setCreateTime(new Date());
					taskUsers.setUpdateTime(new Date());
					taskUsers.setPubTime(new Date());
					this.taskDbService.saveOneUsersService(taskUsers);
					succedNum ++ ;
				}
				
			  
			}
			
				StringBuffer sb = new StringBuffer();
				sb.append("总条数:" + totalNum + ";");
				sb.append("成功条数:" + succedNum + ";");
				sb.append("更新条数:" + failedNum + ";");
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
			}
				return apiResponse.toJSON().toJSONString();
	}

	@Resource
	private ISystemOperService systemOperService;
	
    /**
	    * 发送邮件操作
	  * @return
  	  */
	 @RequestMapping(value = "/taskDescSend" , produces = "text/html;charset=UTF-8")
	 @ResponseBody
	 public String taskDescSend(String id, HttpServletRequest request,Model model)
	 {
		ConstatFinalUtil.SYS_LOGGER.info("--taskDescAdd--");
		ApiResponse<Object> apiResponse=new ApiResponse<Object>();
		boolean targe = this.systemOperService.operUsersSendMailService(id); 
		StringBuffer sb = new StringBuffer(); 
		sb.append("线程已经启动");
		apiResponse.setInfo(sb.toString(), Collections.EMPTY_MAP);
		return apiResponse.toJSON().toJSONString();
	 }

     
}